<template>
  <div>
    <el-card
      shadow="always"
      style="margin-bottom: 20px"
      :body-style="{ padding: '20px' }"
    >
      <!-- card body -->
      <el-form
        :model="userQueryVo"
        label-width="100px"
        :inline="false"
        size="normal"
      >
        <el-row :gutter="20">
          <el-col :span="8"
            ><el-form-item label="用户昵称">
              <el-input
                v-model="userQueryVo.nickName"
                placeholder="请输入搜索用户昵称"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="8">
            <el-form-item label="用户名">
              <el-input
                v-model="userQueryVo.username"
                placeholder="请输入搜索用户名"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6" :offset="16">
            <el-button type="primary" size="default" @click="search"
              >搜索</el-button
            >
            <el-button type="info" size="default" @click="reset"
              >重置</el-button
            >
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <el-card shadow="always" :body-style="{ padding: '20px' }">
      <div slot="header">
        <el-button type="success" size="default" @click="addCount">
          新增账户
        </el-button>
      </div>
      <el-table :data="userTable" border style="width: 100%">
        <el-table-column type="index" label="#" width="50" align="center">
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="180"
          align="center"
        >
        </el-table-column>

        <el-table-column prop="nick_name" label="昵称" align="center">
        </el-table-column>
        <el-table-column label="头像" width="180" align="center">
          <template slot-scope="scope">
            <div class="img">
              <img :src="scope.row.salt" class="img" alt="" align="center" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="roles" label="角色" align="center">
        </el-table-column>
        <el-table-column prop="create_time" label="添加时间" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="default" @click="editRow(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="default"
              @click="delRow(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="start"
        :page-sizes="[5, 10]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="fullData"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="编辑用户" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="formList"
        ref="form"
        label-width="100px"
        :inline="false"
        size="normal"
      >
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="用户名">
              <el-input v-model="formList.username"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="用户昵称">
              <el-input v-model="formList.nick_name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="密码" v-if="flag">
              <el-input v-model="formList.password"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="用户头像">
              <el-upload
                class="avatar-uploader"
                ref="bigPic"
                action="/lejuAdmin/material/uploadFileOss"
                :headers="{ token }"
                :on-success="uploadPicSuccess"
              >
                <img v-if="formList.salt" class="img" :src="formList.salt" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>

          <el-col :span="20">
            <el-form-item label="选择角色">
              <el-select
                v-model="formList.roleIds"
                multiple
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in roleList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="role">确 定</el-button>
      </span>
    </el-dialog>

    <buttomBanner></buttomBanner>
  </div>
</template>

<script>
import buttomBanner from "@/components/buttomBanner";
import {
  findUsersByPage,
  findAllRoles,
  updateUserRoles,
  saveUserRoles,
  removeUser,
} from "@/api/permissions/account/account.js";
import { getToken } from "@/utils/auth.js";
import { getInfo } from "@/utils/auth.js";
export default {
  data() {
    return {
      userQueryVo: {},
      formList: {},
      roleList: [],
      dialogVisible: false,
      userTable: [],
      start: 1,
      limit: 5,
      fullData: 0,
      flag: false,
    };
  },
  computed: {
    token() {
      return getToken();
    },
  },
  components: {
    buttomBanner,
  },
  methods: {
    // 删除当前行
    delRow(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          if (res == "confirm") {
            removeUser(id).then((res) => {
              if (res.success) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.init();
              }
            });
          }
        })
        .catch((res) => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 搜索用户
    search() {
      var a = this.userQueryVo;
      this.init(a);
    },
    // 重置搜索
    reset() {
      this.userQueryVo = {};
    },
    // 弹窗点击确定修改或者新增角色
    role() {
      var api;
      if (this.flag) {
        api = saveUserRoles;
      } else {
        api = updateUserRoles;
      }
      var user = {
        createTime: this.formList.create_time,
        id: this.formList.id,
        isDeleted: this.formList.is_deleted ? true : false,
        modifyTime: this.formList.modify_time,
        nickName: this.formList.nick_name,
        password: this.formList.password,
        roleIds: this.formList.roleIds,
        salt: this.formList.salt,
        username: this.formList.username,
      };
      api(user).then((res) => {
        if (res.success) {
          this.$message.success("更新成功");
          this.init();
          this.dialogVisible = false;
        }
      });
    },
    // 图片上传成功
    uploadPicSuccess(v) {
      if (v.success) {
        this.$forceUpdate();
        this.$refs.bigPic.clearFiles();
        this.formList.salt = v.data.fileUrl;
      }
    },
    // 新增账户
    addCount() {
      this.formList = {};
      this.dialogVisible = true;
      this.flag = true;
    },
    // 一页展示更多数据
    handleSizeChange(v) {
      this.limit = v;
      this.init();
    },
    // 换页
    handleCurrentChange(v) {
      this.start = v;
      this.init();
    },
    // 编辑当前行
    editRow(item) {
      this.dialogVisible = true;
      this.flag = false;
      this.formList = item;
      var a = this.roleList.find((el) => el.label == this.formList.roles);
      // this.formList.roleIds = a.value;

      this.$set(this.formList, "roleIds", a.value);
    },
    // 初始化数据
    init(search) {
      if (search) {
        console.log(search);
        console.log("adnl ");
        findUsersByPage(this.start, this.limit, search).then((res) => {
          if (res.success) {
            this.userTable = res.data.rows;
            this.fullData = res.data.total;
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        console.log(4);
        findUsersByPage(this.start, this.limit).then((res) => {
          if (res.success) {
            this.userTable = res.data.rows;
            this.fullData = res.data.total;
          } else {
            this.$message.error(res.message);
          }
        });
      }
    },
    // 初始化角色选择下拉框
    initRole() {
      findAllRoles().then((res) => {
        var arr = [];
        res.data.items.forEach((el) => {
          arr.push({
            value: el.id,
            label: el.roleName,
          });
        });
        this.roleList.push(...arr);
      });
    },
  },
  created() {
    this.init();
    this.initRole();
  },
};
</script>

<style scoped lang="scss">
.img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  overflow: hidden;
  background-color: #bfa;
  margin: 0 auto;
}
.el-pagination {
  text-align: center;
  margin-top: 10px;
}
</style>